package com.blb.ssh.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.blb.ssh.entity.PageHelp;
import com.blb.ssh.entity.User;
import com.blb.ssh.service.UserService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@Namespace("/user")
@ParentPackage("json-default")
@Scope("prototype")
public class UserAction extends BaseAction {

	private static final long serialVersionUID = 6085782812830041535L;
	
	@Autowired
	private UserService userService;
	
	private int page = 1;
	
	private int rows = 10;
	
	private PageHelp pageHelp;
	
	private User user;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public PageHelp getPageHelp() {
		return pageHelp;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	/**
	 * 用户列表界面
	 * @return
	 */
	@Action(value = "list", results = {
		@Result(name = PAGE_LIST, location = JSP_PATH + "user/user_list.jsp")
	})
	public String userList(){
		return PAGE_LIST;
	}
	
	/**
	 * 用户分页数据
	 * @return
	 */
	@Action(value = "userList", results = {
		@Result(name = SUCCESS, type = JSON, params = {
			ROOT, "pageHelp"	
		})	
	})
	public String getUserList(){
		pageHelp = userService.getUserList(page, rows);
		return SUCCESS;
	}

	/**
	 * 保存用户
	 * @return
	 */
	@Action(value = "saveUser", results = {
		@Result(name = SUCCESS, type = JSON, params = {ROOT, successMsg}),
		@Result(name = ERROR, type = JSON, params = {ROOT, errorMsg})
	})
	public String saveUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String string = request.getParameter("ajaxData");
		JSONObject object = JSONObject.fromObject(string);
		user = (User) JSONObject.toBean(object, User.class);
		List<User> list = userService.findByHQL("from User where username = ?0", user.getUsername());
		if (!list.isEmpty()) {
			setErrorResult("用户已存在！！！");
			return ERROR;
		}
		userService.save(user);
		return SUCCESS;
	}
	
	/**
	 * 删除用户
	 * @return
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	@Action(value = "deleteUser", results = {
		@Result(name = SUCCESS, type = JSON, params = {ROOT, successMsg}),
		@Result(name = ERROR, type = JSON, params = {ROOT, errorMsg})	
	})
	public String deleteUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String string = request.getParameter("users");
		JSONArray jsonArray = JSONArray.fromObject(string);
		List<User> list = JSONArray.toList(jsonArray, User.class);
		for (User user : list) {
			if ("root".equals(user.getUsername())) {
				setErrorResult("root账号不能被删除！！！");
				return ERROR;
			}
		}
		userService.batchDelete(list);
		return SUCCESS;
	}
	
	/**
	 * 修改用户
	 * @return
	 */
	@Action(value = "updateUser", results = {
		@Result(name = SUCCESS, type = JSON, params = {ROOT, successMsg}),
		@Result(name = ERROR, type = JSON, params = {ROOT, errorMsg})
	})
	public String updateUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String string = request.getParameter("ajaxData");
		JSONObject object = JSONObject.fromObject(string);
		user = (User) JSONObject.toBean(object, User.class);
		userService.update(user);
		return SUCCESS;
	}
}
